﻿package com.oyjd.beauty.business.goodsType.action;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.ServletRequestUtils;

import com.oyjd.beauty.bean.vo.GoodsTypeVO;
import com.oyjd.beauty.bean.vo.UserVO;
import com.oyjd.beauty.business.goodsType.service.GoodsTypeService;
import com.oyjd.beauty.fw.action.BaseAction;
import com.oyjd.beauty.fw.action.Page;
import com.oyjd.beauty.fw.util.CodeUtil;
import com.oyjd.beauty.fw.util.OutUtil;
import com.oyjd.beauty.pub.Const;

/**
 * 
 * 商品分类Action控制层
 * 
 * @author <b>欧建东<br/>
 *         Email:oyjd@163.com</b>
 * @version 1.0
 */
@Action(value = "/goodsType")
@Results({ @Result(name = "viewList", location = Const.JSP_goodsType_list) })
public class GoodsTypeAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 45354347027015039L;

	private static final Log log = LogFactory.getLog(GoodsTypeAction.class);
	private GoodsTypeService service;

	@Autowired
	public void setService(GoodsTypeService service) {
		this.service = service;
	}

	/**
	 * 进入商品分类列表页面
	 * 
	 * @return
	 */
	public String viewList() {
		return "viewList";
	}

	/**
	 * 进入商品分类新增页面
	 * 
	 * @return
	 */
	public String viewAdd() {
		return "viewAdd";
	}

	/**
	 * 进入商品分类修改页面
	 * 
	 * @return
	 */
	public String viewEdit() {
		try {
			HttpServletRequest req = getRequest();
			String id = ServletRequestUtils.getStringParameter(req, "id");
			GoodsTypeVO vo = (GoodsTypeVO) service.getGoodsTypeVO(id);
			req.setAttribute("data", vo);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
		}
		return "viewEdit";
	}

	/**
	 * 获取商品分类列表数据
	 * 
	 * @return
	 */
	public String getList() {
		Map<String, Object> map = CodeUtil.getJsonMap();
		try {
			HttpServletRequest req = getRequest();
			Page<GoodsTypeVO> page = service.getList(new Page<GoodsTypeVO>(req));
			//
			map.put("count", page.getCount());
			map.put("info", page.getResult());
			map.put("code", CodeUtil.SUCCESS);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}

	/**
	 * 新增商品分类
	 * 
	 * @return
	 */
	public String add() {
		Map<String, Object> map = CodeUtil.getJsonMap();
		try {
			HttpServletRequest req = getRequest();
			String pid = ServletRequestUtils.getStringParameter(req, "pid");
			String name = ServletRequestUtils.getStringParameter(req, "name");
			String note = ServletRequestUtils.getStringParameter(req, "note");
			UserVO userVO = (UserVO) session.get(CodeUtil.SessionKey);
			service.add(pid, name, note, userVO.getAccount());
			map.put("code", CodeUtil.SUCCESS);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}

	/**
	 * 编辑商品分类
	 * 
	 * @return
	 */
	public String edit() {
		Map<String, Object> map = CodeUtil.getJsonMap();
		try {
			HttpServletRequest req = getRequest();
			String goodsTypeId = ServletRequestUtils.getStringParameter(req, "goodsTypeId");
			String pid = ServletRequestUtils.getStringParameter(req, "pid");
			String name = ServletRequestUtils.getStringParameter(req, "name");
			String note = ServletRequestUtils.getStringParameter(req, "note");

			UserVO userVO = (UserVO) session.get(CodeUtil.SessionKey);
			service.edit(goodsTypeId, pid, name, note, userVO.getAccount());
			map.put("code", CodeUtil.SUCCESS);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}

	/**
	 * 删除商品分类
	 * 
	 * @return
	 */
	public String remove() {
		Map<String, Object> map = CodeUtil.getJsonMap();
		try {
			HttpServletRequest req = getRequest();
			String[] cbs = ServletRequestUtils.getStringParameters(req, "id");
			UserVO userVO = (UserVO) session.get(CodeUtil.SessionKey);
			service.remove(cbs, userVO.getAccount());
			map.put("code", CodeUtil.SUCCESS);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}

	/**
	 * 获取商品类型树数据
	 * 
	 * @return
	 */
	public String getTypeTree() {
		try {
			JSONArray treeJson = service.getTypeTree();
			OutUtil.outJson(treeJson, getResponse());
		} catch (Exception err) {
			log.error(err.getMessage(), err);
		}
		return null;
	}

	/**
	 * 获取类型下拉选择数据
	 * 
	 * @return
	 */
	public String getSelectType() {
		Map<String, Object> map = CodeUtil.getJsonMap();
		try {
			List<GoodsTypeVO> result = service.getSelectType();
			map.put("info", result);
			map.put("code", CodeUtil.SUCCESS);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}

}
